package com.dmc.dao;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.dmc.dto.PayrollDto;
import com.zero.basic.dao.SqlListBaseDao;
import com.zero.basic.dao.SqlPagingListBaseDao;
import com.zero.basic.dao.SqlQueryBaseDao;
import com.zero.basic.exception.SmsDaoException;
import com.zero.basic.model.Pager;
import com.zero.mms.model.EmpSalary;
import com.zero.mms.model.SalaryItem;

/*
 * Dao采用的是字符串拼接，因为另外采用参数填充方式的工具有问题，待完善
 */
@Repository("payrollDao")
@Transactional(rollbackFor = Exception.class)
public class PayrollDaoImpl extends SqlListBaseDao<EmpSalary> implements PayrollDao{

	@Inject
	private SqlListBaseDao<?> sqlListBaseDao;
//	@Inject
//	private SqlPagingListBaseDao<?> sqlPagingListBaseDao;
	
	@Override
	public List<PayrollDto> listPayroll(String depId, String empNo,
			String fromDate, String toDate) throws SmsDaoException {
		System.out.println("---------Dao:"+depId+" "+ empNo+" "+  fromDate+" "+  toDate);
		try {
			String sql = "select d.dep_no as depNo, d.dep_name as depName, e.emp_id as empNo, "
					+ "e.emp_name as empName, s.emp_salary_date as empSalaryDate "
					+ "from t_emp_salary s, t_emp e, t_dep d, t_salary_item i "
					+ "where s.emp_id = e.emp_id and s.salary_item_id = i.salary_item_id "
					+ "and e.dep_id = d.dep_id ";

			if (depId!=null && depId != "") {
				sql += " and d.dep_id="+depId ;
//				sql += " and d.dep_id=?";
			}
			if (!"".equals(empNo)) {
				sql += " and e.emp_id ='" + empNo + "'";
//				sql += " and e.emp_id='?'";
			}
			if (fromDate != null) {
				sql += " and s.emp_salary_date>='"+fromDate+"'";
//				sql += " and s.emp_salary_date>='?'";
			}
			if (toDate != null) {
				sql += " and s.emp_salary_date<='"+toDate+"'";
//				sql += " and s.emp_salary_date<='?'";
			}
			sql += " group by e.emp_id, s.emp_salary_date";

//			Pager<PayrollDto> empSalDtoList = sqlPagingListBaseDao.findBySql(sql,
//					new Object[]{depId, empNo, fromDate, toDate}, PayrollDto.class, false);
//			Pager<PayrollDto> empSalDtoList = new Pager();
			List<PayrollDto> datas = sqlListBaseDao.listBySql(sql, null, PayrollDto.class, false, true);
//			empSalDtoList.setDatas(datas);  
			return datas;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsDaoException();
		}
	}

	@Override
	public List<SalaryItem> listPayrollItems() throws SmsDaoException {
		try {
			String sql = "select i.* from t_salary_item i ";
			List<SalaryItem> salaryItemList = sqlListBaseDao.listBySql(sql,
					SalaryItem.class, true, true);
			return salaryItemList;
		} catch (Exception e) {
			throw new SmsDaoException();
		}
	}

}
